package api.cie.rss.dal;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * The class to manipulate with the Article table in the database
 * @author STL-API
 *
 */
public class ArticleDAL {

	public static final String KEY_ROWID = "_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_URL = "url";
	public static final String KEY_DESCRIPTION = "description";	
	public static final String KEY_IDRSS = "idRSS";
	public static final String KEY_RATINGS = "ratings";
	public static final String KEY_DATE = "date";
	
	private static final String DATABASE_TABLE = "Article";
	private SQLiteDatabase database;
	
	public ArticleDAL(DatabaseAdapter dbAdapter) {
		database = dbAdapter.getDatabase();
	}
	
	/**
	 * Create a new Article. If the Article is successfully created, return the new
	 * rowId for that Rss, otherwise return a -1 to indicate failure.
	 */
	public long createArticle(String title, String description, String url, int idRSS,
			int ratings, String date) {
		ContentValues initialValues = createContentValues(title, description,
				url, idRSS, ratings, date);

		return database.insert(DATABASE_TABLE, null, initialValues);
	}
	
	private ContentValues createContentValues(String title, String description, String url, int idRSS,
			int ratings, String date) {
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, title);
		values.put(KEY_DESCRIPTION, description);
		values.put(KEY_URL, url);
		values.put(KEY_IDRSS, idRSS);
		values.put(KEY_RATINGS, ratings);
		values.put(KEY_DATE, date);
		
		return values;
	}
	
	/**
	 * Deletes article
	 */
	public boolean deleteArticle(long rowId) {
		return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	/**
	 * Delete all articles in the database
	 */
	public void deleteAllArticle() {
		database.delete(DATABASE_TABLE, null, null);
	}

	/**
	 * Return a Cursor over the list of all articles in the database
	 * 
	 * @return Cursor over all articles
	 */
	public Cursor fetchAllArticles() {
		return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_TITLE, KEY_DESCRIPTION, KEY_URL, KEY_IDRSS, KEY_RATINGS, KEY_DATE },
				null, null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the defined article
	 */
	public Cursor fetchArticle(long rowId) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
				KEY_TITLE, KEY_DESCRIPTION, KEY_URL, KEY_IDRSS, KEY_RATINGS, KEY_DATE },
				KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	/**
	 * Return a Cursor over the list of all articles belonging to a defined RSS
	 */
	public Cursor fetchArticlesByRSS(int idRSS) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
				KEY_TITLE, KEY_DESCRIPTION, KEY_URL, KEY_IDRSS, KEY_RATINGS, KEY_DATE },
				KEY_IDRSS + "=" + idRSS, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	/**
	 * Return a Cursor over the list of all articles with the title containing the string 
	 */
	public Cursor fetchArticlesByTitle(String title) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
				KEY_TITLE, KEY_DESCRIPTION, KEY_URL, KEY_IDRSS, KEY_RATINGS, KEY_DATE },
				KEY_TITLE + " LIKE '%" + title + "%'", null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean deleteArticleByRSS(long rssId){
		return database.delete(DATABASE_TABLE, KEY_IDRSS + "=" + rssId, null) > 0;
	}
	
}
